package com.foreknow.elm.dao.impl;

import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/15 21:41
 */
public class OrderdetailetDaoImpl implements OrderdetailetDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public int addOrderdetailet(List<Orderdetailet> list) throws Exception{
        int isRight=0;
        //insert into xxx values(xxx,xxx,xxx),(xxx,xxx,xxx),(xxx,xxx,xxx)
        //StringBuffer stringBuffer = new StringBuffer("insert into orderDetailetorderId,foodId,quantity)values");
        //for(OrderDetailet od;list) {
        //stringBuffer.append("("+od.get0rderId()+","+od.getFoodId()+","+od.getQuantity()+"),");
        //}
        //去掉sql中最后一个逗号
        // String sql = stringBuffer.toString().substring(0,stringBuffer,toString().length()-1):
        String sql ="insert into orderdetailet(orderId,foodId,quantity) values(?,?,?)";
        try {
            con = DBUtil.getConnection();
            for (int i = 0; i < list.size(); i++) {
                pst = con.prepareStatement(sql);
                pst.setInt(1, list.get(i).getOrderId());
                pst.setInt(2, list.get(i).getFoodId());
                pst.setInt(3, list.get(i).getQuantity());
                isRight = pst.executeUpdate();
            }
        }finally {  //这里不能处理异常，也就是没有catch，只有finally
            //这里负责关闭PreparedStatement和ResultSet
            DBUtil.close(rs, pst);
        }
        return isRight;
    }

    @Override
    public List<Orderdetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception {
        List<Orderdetailet> list=new ArrayList<>();
        StringBuffer sql=new StringBuffer();
        sql.append(" select o.*, ");
        sql.append(" f.foodId ffoodId, ");
        sql.append(" f.foodName ffoodName, ");
        sql.append(" f.foodPrice ffoodPrice ");
        sql.append(" from orderDetailet o left join food f on o.foodId=f.foodId ");
        sql.append(" where o.orderId=? ");
       try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql.toString());
            pst.setInt(1,orderId);
            rs=pst.executeQuery();
            while (rs.next()){
                Orderdetailet od=new Orderdetailet();
                od.setOdId(rs.getInt("odId"));
                od.setOrderId(rs.getInt("orderId"));
                od.setFoodId(rs.getInt("foodId"));
                od.setQuantity(rs.getInt("quantity"));

                Food food=new Food();
                food.setFoodId(rs.getInt("ffoodId"));
                food.setFoodName(rs.getString("ffoodName"));
                food.setFoodPrice(rs.getDouble("ffoodPrice"));
                //设置关联关系
                od.setFood(food);
                list.add(od);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
